﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace odinshrine
{
    class map
    {
        public const int cote_caisse = 34;
        public const int taille_case = 34;

        public Elements[,] caisseList2 = new Elements[(800 / taille_case) * 10, 600 / taille_case];
        public Structure[] structlist = new Structure[400];
        public Structure currentStruct = new Structure();
        public Elements plateforme, caisse;
        public int structCounter = 0;

        string typeObj;
        string path;

        public int coefx = 0, coefy = 0;
        /*
        public table2d caisseList = new table2d(500);
        int k = 0;
         */

        
        public map(string path)
        {
            this.path = path;
        }

        public void Init()
        {
            for (int y = 0; y< caisseList2.GetLength(1); y++)
            {
                for (int x = 0; x < caisseList2.GetLength(0); x++)
                {
                    caisseList2[x, y] = null;
                }
            }
        }
        
    /*    public void Load(ContentManager content)
        {
            caisseList.Init();

            using (StreamReader sr = File.OpenText(path))
            {
                string line;
                int start, end, type, x, y;

                for (int i = 0; (line = sr.ReadLine()) != null; i++)
                {
                    start = 0;
                    for (end = start; line[end] != ' '; end++) ;
                    type = int.Parse(line.Substring(start, (end - start)));

                    start = end + 1;
                    for (end = start; line[end] != ' '; end++) ;
                    x = int.Parse(line.Substring(start, (end - start)));

                    start = end + 1;
                    for (end = start; line[end] != ' ' && end < line.Length - 1; end++) ;
                    y = int.Parse(line.Substring(start, (end - start + 1)));

                    caisseList.tableau[i, 0] = type;
                    caisseList.tableau[i, 1] = x;
                    caisseList.tableau[i, 2] = y;
                    k = i;
                }
                k++;

                sr.Close();
            }
        }
     */
        
        public void Load2(ContentManager content, GestionEnnemi ennemi)
        {
            caisse = new Elements(content.Load<Texture2D>(@"images/caisse"),'C',Elts_T.Simple, Elts_P.None);
            plateforme = new Elements(content.Load<Texture2D>( @"images/plateform"), 'o', Elts_T.Plateform, Elts_P.All);

            using (StreamReader sr = File.OpenText(path))
            {
                string line;

                for (int y = 0; y < caisseList2.GetLength(1) && (line = sr.ReadLine()) != null; y++)
                {
                    for (int x = 0; x < caisseList2.GetLength(0) && x < line.Length; x++)
                    {
                        if (line[x] == 'C')
                        {
                            putAThing(new Elements(caisse), x, y);
                        }
                        else if (line[x] == 'o')
                        {
                            plateforme.sprte_e.ChangeAnime("Mid", true);
                            putAThing(new Elements(plateforme), x, y);
                        }
                        else if (line[x] == '<')
                        {
                            plateforme.sprte_e.ChangeAnime("Left", true);
                            putAThing(new Elements(plateforme), x, y);
                        }
                        else if (line[x] == '>')
                        {
                            plateforme.sprte_e.ChangeAnime("Right",true);
                            putAThing(new Elements(plateforme), x, y);
                        }
                        else if (line[x] == '.')
                        {

                        }
                        else if (line[x] == 'e')
                        {
                            PutEnnemy(ennemi, new Soldat(content),
                               x * taille_case - coefx, y * taille_case - coefy - content.Load<Texture2D>("images/ennemi").Height + 34); //34 = taille du carre dans l editeur
                        }
                        else
                        {
                            //throw (new Exception());
                        }
                    }
                }
                sr.Close();
            }
        }

        private void PutEnnemy(GestionEnnemi ennemi, BadGuys badguy, int posX, int posY)
        {
            ennemi.AddEnnemi(badguy);
            ennemi.tabEnnemi1.Last.Value.Position.X = posX;
            ennemi.tabEnnemi1.Last.Value.Position.Y = posY;
        }

        public void putAThing(Elements Elts, int x, int y)
        {
            if (Elts.type == Elts_T.Plateform)
            {
                if (x > 0 && Elts.sprte_e.SpriteAnime != "Right")
                {
                    {
                        if (currentStruct.counter == 0)
                        {
                        }
                        else
                        {
                            //  structlist[structCounter] = new Structure(currentStruct);
                            //   currentStruct.Reset();
                        }
                    }




                    currentStruct.Add(Elts, new Vector2(x * taille_case, y * taille_case), caisseList2);
                    Elts.structIndic = structCounter;
                }
                else
                {

                    if (currentStruct.counter != 0)
                    {
                        //          structlist[structCounter] = new Structure(currentStruct);
                        //         structCounter++;

                    }
                    // currentStruct.Reset();
                    currentStruct.Add(Elts, new Vector2(x * taille_case, y * taille_case), caisseList2);
                    structlist[structCounter] = new Structure(currentStruct);
                    Elts.structIndic = structCounter;
                    structCounter++;
                    currentStruct.Reset();
                }
            }
            else
            {
                Elts.structIndic = structCounter;
                structCounter++;
            }
            caisseList2[x, y] = new Elements(Elts);
        }


   /*     public void Draw(SpriteBatch spriteBatch, GraphicsDeviceManager graphics)
        {
            Vector2 coord = new Vector2(0, 0);

            for (int i = 0; i < caisseList.nbre_cases && caisseList.tableau[i, 1] != -1 && caisseList.tableau[i, 2] != -1; i++)
            {
                if (caisseList.tableau[i, 1] - coefx + taille_case >= 0 && caisseList.tableau[i, 1] - coefx <= graphics.PreferredBackBufferWidth && caisseList.tableau[i, 2] - coefy <= graphics.PreferredBackBufferHeight && caisseList.tableau[i, 2] - coefy + taille_case >= 0)
                {
                    if (caisseList.tableau[i, 0] == 1)
                    {
                        coord.X = caisseList.tableau[i, 1] - coefx;
                        coord.Y = caisseList.tableau[i, 2] - coefy;

                        spriteBatch.Draw(caisse.Texture, coord, Color.White);
                    }
                }
            }
        }
     */

        public void Draw2(SpriteBatch spriteBatch, GraphicsDeviceManager graphics, GestionEnnemi ennemi)
        {
            Vector2 coord = Vector2.Zero;
            
            for (int y = 0; y < caisseList2.GetLength(1); y++)
            {
                for (int x = 0; x < caisseList2.GetLength(0); x++)
                {
                    if (caisseList2[x, y] != null && (x * taille_case) - coefx < graphics.PreferredBackBufferWidth && (y * taille_case) - coefy < graphics.PreferredBackBufferHeight - taille_case && (x * taille_case) - coefx >= -taille_case && (y * taille_case) - coefy >= -taille_case)
                    {
                        coord.X = (x * taille_case) - coefx;
                        coord.Y = (y * taille_case) - coefy;
                        caisseList2[x, y].Draw(spriteBatch,(int) coord.X,(int)coord.Y);
                    }
                }
            }
        }
    }
}
